/*
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * RSElement.h
 *
 *  Created on: 2010/02/16
 *      Author: Alexandre Pires ( c.alexandre.pires@gmail.com )
 */

#ifndef RSELEMENT_H_
#define RSELEMENT_H_

#include "RSObject.h"

namespace Realsphere {

    class RSElement: public RSObject {
        Q_OBJECT
        public:
            RSElement ( const QString& rTag = RS_ELEMENT_TAG );
            virtual ~RSElement();

            QString                 Tag () const;
            RSElement*              RootElement () const;
            RSElement*              ParentElement () const;
            RSElement*              GetChildElement ( unsigned int rIndex ) const;
            int                     IndexOf ( RSElement* rElement );
            unsigned int            ChildElementsSize ();
            RSElements              ChildElements () const;
            RSElementsIterator      ChildElementsIterator () const;
            virtual bool            IsRootElement () const = 0;

        public:
            template <class T>
            QList<T>                FindChildsElements ( const QString& rName = QString::null ) const;
            template <class T>
            T                       FirstChildElement ( const QString& rName = QString::null ) const;

        public:
            virtual void            WriteXml ( QXmlStreamWriter* rXmlWriter );

        public:
            RSElement*              operator[] ( int rIndex );

        public slots:
            void                    SetTag ( const QString& rTag, bool rEmit = true );

        protected slots:
            RSError                 AddChildElement ( RSElement* rChild );
            RSError                 RemoveChildElement ( RSElement* rChild );
            RSError                 RemoveChildElement ( unsigned int rIndex );
            RSError                 SetRootElement ( RSElement* rRootElement, bool rNotify = true );

        signals:
            void                    ChildElementAdded ( RSElement* rElement, RSElement* rChild );
            void                    ChildElementRemoved ( RSElement* rElement, RSElement* rChild );
            void                    RootElementChanged ( RSElement* rElement, RSElement* rRootElement );
            void                    TagNameChanged ( RSElement* rElement, const QString& rTag );

        protected:
            virtual void            WriteElementAttributes ( QXmlStreamWriter* rXmlWriter );
            virtual void            WriteElementChilds ( QXmlStreamWriter* rXmlWriter );
            virtual void            WriteElementData ( QXmlStreamWriter* rXmlWriter );
            virtual RSError         BeforeAddChildElement ( RSElement* rChild );
            virtual RSError         BeforeRemoveChildElement ( RSElement* rChild );
            virtual bool            AfterAddChildElement ( RSElement* rChild );
            virtual bool            AfterRemoveChildElement ( RSElement* rChild );
            void                    SetParentElement ( RSElement* rChild );

        private:
            QString                 mTag;
            RSElement*              mRootElement;
            RSElements              mChildElements;
            RSElement*              mParentElement;
    };

    template <class T>
    QList<T> RSElement::FindChildsElements ( const QString& rName ) const {
            return findChildren<T>( rName );
    }

    template <class T>
    T RSElement::FirstChildElement ( const QString& rName ) const {
            return findChild<T>( rName );
    }
}

#endif /* RSELEMENT_H_ */
